//
//  ERefreshFooter.m
//  Common
//
//  Created by guoshiqing on 2018/6/15.
//  Copyright © 2018年 www.bitkinetic.com. All rights reserved.
//

#import "ERefreshFooter.h"

@interface ERefreshFooter ()

@property (weak, nonatomic) UIImageView *loadingImageV;
@property (weak, nonatomic) UILabel *label;

@end

@implementation ERefreshFooter

- (void)prepare
{
    [super prepare];
    
    // 设置控件的高度
    self.mj_h = 50;
    
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor colorWithRed:114/255.0 green:114/255.0 blue:114/255.0 alpha:1.0f];
    label.font = [UIFont boldSystemFontOfSize:14];
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    self.label = label;
    
    UIImageView *logoImgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"refresh_load"]];
    logoImgv.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:logoImgv];
    self.loadingImageV = logoImgv;
    
}

- (void)placeSubviews
{
    [super placeSubviews];
    
    self.loadingImageV.center = CGPointMake(self.mj_w * 0.5 , self.mj_h  * 0.5);
    self.label.bounds = self.bounds;
    self.label.center = CGPointMake(self.mj_w * 0.5 , self.mj_h  * 0.5);
}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change {
    [super scrollViewContentOffsetDidChange:change];
}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change {
    [super scrollViewContentSizeDidChange:change];
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change {
    [super scrollViewPanStateDidChange:change];
}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state {
    MJRefreshCheckState;
    
    switch (state) {
        case MJRefreshStateIdle:
            [self stopAniamting];
            [self performSelector:@selector(hiddenResultLab) withObject:nil afterDelay:1.0];
            break;
        case MJRefreshStatePulling:
            [self stopAniamting];
            break;
        case MJRefreshStateRefreshing:
            [self startAnimating];
            break;
        default:
            break;
    }
}

- (void)hiddenResultLab
{
    self.label.hidden = YES;
}
//开始无穷旋转
- (void)startAnimating
{
    self.loadingImageV.hidden = NO;
    self.label.hidden = YES;
    [self.loadingImageV setTransform:CGAffineTransformMakeScale(1.0f, 1.0f)];
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = @(M_PI * 2.0);
    rotationAnimation.duration = 0.6f;
    rotationAnimation.autoreverses = NO;
    rotationAnimation.repeatCount = HUGE_VALF;
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [self.loadingImageV.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    [self.loadingImageV sizeToFit];
}

- (void)stopAniamting
{
    self.loadingImageV.hidden = YES;
    self.label.hidden = NO;
    [self.loadingImageV.layer removeAnimationForKey:@"rotationAnimation"];
}

- (void)endFooterRefreshingWithRetunResultSucceed:(int)result
{
    if (self.state != MJRefreshStateRefreshing) {
        return;
    }
    
    [self stopAniamting];
    
    switch (result) {
        case 0:
            self.label.text = @"刷新失败";
            break;
        case 1:
            self.label.text = @"刷新成功";
            break;
        case 2:
            self.label.text = @"已加载全部邮件";
            break;
        case 3:
            self.label.text = @"没有更多的数据了";
            break;
        default:
            break;
    }
    
    [self performSelector:@selector(endRefreshing) withObject:nil afterDelay:1.0f];
}

- (void)endFooterRefreshingWithResult:(int)result
{
    if (self.state != MJRefreshStateRefreshing) {
        return;
    }
    
    [self stopAniamting];
    
    switch (result) {
        case 0:
            self.label.text = @"刷新失败";
            break;
        case 1:
            self.label.text = @"刷新成功";
            break;
        case 2:
            self.label.text = @"已加载全部";
            break;
        case 3:
            self.label.text = @"没有更多的数据了";
            break;
        default:
            break;
    }
    
    [self performSelector:@selector(endRefreshing) withObject:nil afterDelay:1.0f];
}
@end
